<?php

namespace App\Http\Controllers\excel;

use App\Http\Controllers\Controller;
use App\Models\Mysql\XnMerchantBill;
use App\Models\Mysql\XnOrder;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;

class GoodsController extends Controller
{
    //订单导出
    public function orderExcel(Request $request){
        echo 111111111112333341232132144421113;exit;
        $params = $request->all();
        $user = auth('merchant')->user();
        $where = [];
        $where[] = ['Merchant_number',$user->merchant_number];
        $where[] = ['recharge_time','>=',$params['start_time']];
        $where[] = ['recharge_time','<=',$params['end_time']];
        if (!empty($params['goods_name'])){
            $where[] = ['goods_name','like','%'.$params['goods_name'].'%'];
        }
        if (null != $params['recharge_status']){
            $where[] = ['recharge_status',$params['recharge_status']];
        }
        if (!empty($params['goods_id'])){
            $where[] = ['goods_id',$params['goods_id']];
        }
        if (!empty($params['account'])){
            $where[] = ['account',$params['account']];
        }

        $res = XnOrder::where($where)
            ->orderBy('recharge_time', 'desc')
            ->select('id','goods_code','account','order_sn','price','goods_num','all_money','recharge_status','recharge_time','update_time','goods_belong','msg','goods_name')
            ->get();

        if (!$res) {
            return $this->sendError('查询失败');
        }
        return $this->sendResponse($res,'查询成功');


    }
    //账单导出
    public function orderBillExcel(Request $request){
        $params = $request->all();
        $user = auth('merchant')->user();
        $where = [];
        if (!empty($params['order_sn'])){
            $where[] = ['merchant_order',$params['order_sn']];
        }
        if (!empty($params['status'])){
            $where[] = ['status',$params['status']];
        }
        $where[] = ['time','>=',$params['start_time']];
        $where[] = ['time','<=',$params['end_time']];
        $where[] = ['merchant_number','=',$user->merchant_number];
        $res = XnMerchantBill::where($where)->orderBy('time', 'desc')->get();
        foreach ($res as $k=>$v){
            if ($v['status'] == 1){
                $res[$k]['goods_name'] = '财务加款';
            }elseif($v['status'] == 4 ){
                $res[$k]['goods_name'] = '财务扣款';
            }else{
                $res[$k]['goods_name'] = XnOrder::where('order_sn',$v['merchant_order'])->value('goods_name');
            }
        }
        return $this->sendResponse($res, '查询成功');
    }

}
